<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2018/11/7
 * Time: 14:43
 */
namespace common\services\tool;

class ToolIdentity
{
    /**
     * 隐藏身份证，并展示前后几位，中间的隐藏
     * @param $number
     * @return string
     */
    public static function idCardHide($number)
    {
        if (!preg_match("/^(\d{18,18}|\d{15,15}|\d{17,17}(?:\d|x|X))$/", $number)) {
            return $number;
        }

        return substr($number, 0, 2) . ' **** **** **** ' . substr($number, -4, 4);
    }

    /**
     * 代码层面验证身份证号是否正确
     * @param $number
     * @return bool|string
     */
    public static function isIDCard($number)
    {
        if (!preg_match("/^(\d{18,18}|\d{15,15}|\d{17,17}(?:\d|x|X))$/", $number)) {
            return false;
        }
        // 检查是否是身份证号
        // 转化为大写，如出现x
        $number = strtoupper($number);
        if (!(strlen($number) == 15 || strlen($number) == 18)) {
            return false;
        }

        // 加权因子
        $wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        // 校验码串
        $ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');

        // 按顺序循环处理前17位
        $sigma = 0;
        for ($i = 0; $i < 17; $i++) {
            // 提取前17位的其中一位，并将变量类型转为实数
            $b = (int)$number{$i};          // 提取相应的加权因子
            $w = $wi[$i];                   // 把从身份证号码中提取的一位数字和加权因子相乘，并累加
            $sigma += $b * $w;
        }
        // 计算序号
        $snumber = $sigma % 11;
        // 按照序号从校验码串中提取相应的字符。
        $check_number = $ai[$snumber];
        if ($number{17} == $check_number) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据身份证号，自动返回对应的星座
     * @param $cid
     * @return string
     */
    public static function getStarSign($cid)
    {
        if (!self::isIDCard($cid)) return '';
        $bir      = substr($cid, 10, 4);
        $month    = (int)substr($bir, 0, 2);
        $day      = (int)substr($bir, 2);
        $strValue = '';
        if (($month == 1 && $day <= 21) || ($month == 2 && $day <= 19)) {
            $strValue = "水瓶座";
        } else if (($month == 2 && $day > 20) || ($month == 3 && $day <= 20)) {
            $strValue = "双鱼座";
        } else if (($month == 3 && $day > 20) || ($month == 4 && $day <= 20)) {
            $strValue = "白羊座";
        } else if (($month == 4 && $day > 20) || ($month == 5 && $day <= 21)) {
            $strValue = "金牛座";
        } else if (($month == 5 && $day > 21) || ($month == 6 && $day <= 21)) {
            $strValue = "双子座";
        } else if (($month == 6 && $day > 21) || ($month == 7 && $day <= 22)) {
            $strValue = "巨蟹座";
        } else if (($month == 7 && $day > 22) || ($month == 8 && $day <= 23)) {
            $strValue = "狮子座";
        } else if (($month == 8 && $day > 23) || ($month == 9 && $day <= 23)) {
            $strValue = "处女座";
        } else if (($month == 9 && $day > 23) || ($month == 10 && $day <= 23)) {
            $strValue = "天秤座";
        } else if (($month == 10 && $day > 23) || ($month == 11 && $day <= 22)) {
            $strValue = "天蝎座";
        } else if (($month == 11 && $day > 22) || ($month == 12 && $day <= 21)) {
            $strValue = "射手座";
        } else if (($month == 12 && $day > 21) || ($month == 1 && $day <= 20)) {
            $strValue = "魔羯座";
        }
        return $strValue;
    }

    /**
     * 根据身份证号，自动返回对应的生肖
     * @param $cid
     * @return string
     */
    public static function getAnimalSign($cid)
    {
        if (!self::isIDCard($cid)) return '';
        $start = 1901;
        $end = $end = (int)substr($cid, 6, 4);
        $x = ($start - $end) % 12;
        $value = "";
        if ($x == 1 || $x == -11) {
            $value = "鼠";
        }
        if ($x == 0) {
            $value = "牛";
        }
        if ($x == 11 || $x == -1) {
            $value = "虎";
        }
        if ($x == 10 || $x == -2) {
            $value = "兔";
        }
        if ($x == 9 || $x == -3) {
            $value = "龙";
        }
        if ($x == 8 || $x == -4) {
            $value = "蛇";
        }
        if ($x == 7 || $x == -5) {
            $value = "马";
        }
        if ($x == 6 || $x == -6) {
            $value = "羊";
        }
        if ($x == 5 || $x == -7) {
            $value = "猴";
        }
        if ($x == 4 || $x == -8) {
            $value = "鸡";
        }
        if ($x == 3 || $x == -9) {
            $value = "狗";
        }
        if ($x == 2 || $x == -10) {
            $value = "猪";
        }
        return $value;
    }

    /**
     * 获取生日的时间戳
     * @param $IDCard
     * @return false|int
     */
    public static function getBirthday($IDCard)
    {
        if (strlen($IDCard) == 18) {
            $tyear  = intval(substr($IDCard, 6, 4));
            $tmonth = intval(substr($IDCard, 10, 2));
            $tday   = intval(substr($IDCard, 12, 2));
            if ($tyear > date("Y") || $tyear < (date("Y") - 100)) {
                $flag = 0;
            } elseif ($tmonth < 0 || $tmonth > 12) {
                $flag = 0;
            } elseif ($tday < 0 || $tday > 31) {
                $flag = 0;
            } else {
                if ((time() - mktime(0, 0, 0, $tmonth, $tday, $tyear)) > 18 * 365 * 24 * 60 * 60) {
                    $flag = 0;
                } else {
                    $flag = 1;
                }
            }
            $tdate = $tyear . "-" . $tmonth . "-" . $tday;
        } else {
            $tyear  = intval("19" . substr($IDCard, 6, 2));
            $tmonth = intval(substr($IDCard, 8, 2));
            $tday   = intval(substr($IDCard, 10, 2));
            if ($tyear > date("Y") || $tyear < (date("Y") - 100)) {
                $flag = 0;
            } elseif ($tmonth < 0 || $tmonth > 12) {
                $flag = 0;
            } elseif ($tday < 0 || $tday > 31) {
                $flag = 0;
            } else {
                if ((time() - mktime(0, 0, 0, $tmonth, $tday, $tyear)) > 18 * 365 * 24 * 60 * 60) {
                    $flag = 0;
                } else {
                    $flag = 1;
                }
            }
            $tdate = $tyear . "-" . $tmonth . "-" . $tday;
        }
        $result['is_adult']   = $flag;                                        // 0标示成年，1标示未成年
        $result['b_day']      = $tdate;                                       // 生日日期
        $result['b_time']     = strtotime($tdate);                            // 生日时间戳
        $result['age']        = self::getAge(strtotime($tdate));              // 年龄
        $result['day_passed'] = ToolTime::getDayPassed(strtotime($tdate));    // 出生多少天
        return $result;
    }

    /**
     * 根据时间戳获取年龄
     * @param $birthday
     * @return false|int|string
     */
    public static function getAge($birthday)
    {
        $year      = date('Y', $birthday);
        $month     = date('m', $birthday);
        $day       = date('d', $birthday);
        $now_year  = date('Y', time());
        $now_month = date('m', time());
        $now_day   = date('d', time());
        $age       = $now_year - $year - 1;
        if ($now_month > $month || ($now_day >= $day && $now_month == $month)) {
            $age += 1;
        }
        return $age;
    }

    /**
     * 根据身份证号，自动返回性别
     * @param $cid
     * @return int 0 男 1 女
     */
    public static function getSexSign($cid)
    {
        $sexInt = (int)substr($cid, 16, 1);
        return $sexInt % 2 === 0 ? 0 : 1;
    }

    /**
     * 根据要求请求的数字返回相对应的省份（固定不变）
     * @param null $number  请求的地域编号（编号规则根据身份证匹配原则）
     * @return string       返回相对应的省份
     */
    public static function provinceNum($number = null)
    {
        switch ($number) {
            case "11": return "北京市";
            case "12": return "天津市";
            case "13": return "河北省";
            case "14": return "山西省";
            case "15": return "内蒙古自治区";
            case "21": return "辽宁省";
            case "22": return "吉林省";
            case "23": return "黑龙江省";
            case "31": return "上海市";
            case "32": return "江苏省";
            case "33": return "浙江省";
            case "34": return "安徽省";
            case "35": return "福建省";
            case "36": return "江西省";
            case "37": return "山东省";
            case "41": return "河南省";
            case "42": return "湖北省";
            case "43": return "湖南省";
            case "44": return "广东省";
            case "45": return "广西壮族自治区";
            case "46": return "海南省";
            case "50": return "重庆市";
            case "51": return "四川省";
            case "52": return "贵州省";
            case "53": return "云南省";
            case "54": return "西藏自治区";
            case "61": return "陕西省";
            case "62": return "甘肃省";
            case "63": return "青海省";
            case "64": return "宁夏回族自治区";
            case "65": return "新疆维吾尔自治区";
            case "71": return "台湾省";
            case "82": return "澳门特别行政区";
            case "81": return "香港特别行政区";
            default: return "其他";
        }
    }
}